package cc.admin.common.db.util;

import cc.admin.common.db.table.entity.DbColumn;
import cc.admin.common.db.table.entity.DbEntity;
import cc.admin.common.db.table.entity.DbColumnType;
import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

import java.io.StringWriter;
import java.util.List;
import java.util.Map;

/**
 * @author: ZhangHouYing
 * @date: 2018-06-24
 */
@Slf4j
public class FreemarkerUtil {

	private static Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
	private static Converter<String, String> converterUpperCamel = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);

	public static String renderContent(String name, Map<String, Object> map) {
		try {
			StringWriter stringWriter = new StringWriter();
			configuration.setClassForTemplateLoading(FreemarkerUtil.class, "template");
			configuration.setNumberFormat("#");
			Template template = configuration.getTemplate(name, "utf-8");
			template.process(map, stringWriter);
			return stringWriter.toString();
		} catch (Exception exception) {
			log.error(exception.getMessage(), (Throwable) exception);
			return exception.toString();
		}
	}

	public static void main(String[] args) {
		Map<String, Object> map = Maps.newHashMap();
		map.put("typeConvertUtil", new TypeConvertUtil());
		DbEntity dbEntity = new DbEntity();
		dbEntity.setId("bi_view");
		dbEntity.setName("视图");
		map.put("entity", dbEntity);
		List<DbColumn> dbColumnList = Lists.newArrayList();
		DbColumn col1 = new DbColumn();
		col1.setEntity("bi_view");
		col1.setDbColumnType(DbColumnType.TEXT);
		col1.setCode("name");
		col1.setName("姓名");
		dbColumnList.add(col1);
		map.put("columnList", dbColumnList);
		String createTableSql = FreemarkerUtil.renderContent("hive.ftl", map);
		System.out.println(createTableSql);
	}

}

